﻿//namespace OppoAds 
//{
using System;
using UnityEngine;

/// <summary>
///manager for native ad and express ad.
/// </summary>
public class AndroidNativeAdManager : INativeAdManager{
        
        protected readonly AndroidJavaObject nativeAdManager;
        
        /// <summary>
        /// Gets the unity main activity.
        /// </summary>
        internal static AndroidJavaObject GetActivity()
        {
            if (s_activity == null)
            {
                var unityPlayer = new AndroidJavaClass(
                    "com.unity3d.player.UnityPlayer");
                s_activity = unityPlayer.GetStatic<AndroidJavaObject>(
                    "currentActivity");
            }

            return s_activity;
        }
        
        private static AndroidJavaObject s_activity;
        private INativeAdManager _nativeAdManagerImplementation;
        AndroidJavaObject activity => GetActivity();
        
        /// <summary>
        /// Initializes a new instance of the <see cref="NativeAd"/> class.
        /// </summary>
        public AndroidNativeAdManager()
        {
            var jc = new AndroidJavaClass(
                       "com.feiya.android.NativeAdManager");
            AndroidJavaObject manager = jc.CallStatic<AndroidJavaObject>("getNativeAdManager");
            this.nativeAdManager = manager;
        }


        public void LoadBanner()
        {
            CallMethod("loadBanner");
        }

        public void ShowBanner()
        {
            CallMethod("showBanner");
        }

        public void CloseBanner()
        {
            CallMethod("closeBanner");
        }

        public void LoadRectangleAd()
        {
            CallMethod("loadRectangleAd");
        }

        public void ShowRectangleAd()
        {
            CallMethod("showRectangleAd");
        }

        public void CloseRectangleAd()
        {
            CallMethod("closeRectangleAd");
        }

        public void LoadFullScreenAd()
        {
            CallMethod("loadFullScreenAd");
        }

        public void ShowFullScreenAd()
        {
            CallMethod("showFullScreenAd");
        }

        public void CloseFullScreenAd()
        {
            CallMethod("closeFullScreenAd");
        }

        public void LoadRewardVideoAd()
        {
            CallMethod("loadRewardVideoAd");
        }

        /// <summary>
        /// Show the reward video Ad.
        /// </summary>
        public void ShowRewardVideoAd()
        {
            CallMethod("showRewardVideoAd");
        }

        public bool isVideoReady()
        {
            return true;// activity.Call<bool>("isVideoReady");
        }

        public bool isInterstitialReady()
        {
            return true;//activity.Call<bool>("isInterstitialReady");
        }

        public void ShowSplash()
        {
            CallMethod("showSplashAd");
        }

        void CallMethod(string methodName)
        {
            if (activity != null)
            {
                var runnable = new AndroidJavaRunnable(
                    () => this.nativeAdManager.Call(methodName, activity));
                activity.Call("runOnUiThread", runnable);
            }
        }
        
    }

//}